//
//  StylistViewController.m
//  MyDIYHouse
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 李平洋. All rights reserved.
//

#import "StylistViewController.h"
#import "StylistListTableViewCell.h"
#import "StylistModel.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "StylistDetailViewController.h"
#import "MJRefresh.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface StylistViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *stylistTableView;

@property (nonatomic,retain)NSMutableArray *stylistArr;
//接口的几个参数写成属性
@property (nonatomic,assign)NSInteger start;
@property (nonatomic,copy)NSString *count;
@property (nonatomic,copy)NSString *user;
@property (nonatomic,copy)NSString *city;



@end

@implementation StylistViewController

- (void)dealloc
{
    self.stylistTableView.delegate = nil;
    [self.stylistTableView release];
    [self.stylistArr release];
    [self.count release];
    [self.user release];
    [self.city release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.stylistArr = [NSMutableArray array];
        self.start = 0;
        self.count = @"5";
        self.user = @"(null)";
        self.city = @"北京";
        [self createData];
    }
    return self;
}


#pragma mark 解析数据的方法
- (void)createData
{
    
    //用拼接字符串的方式,根据参数获取接口的字符串
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/professionals?start=%ld&count=%@&user=%@&city=%@",self.start,self.count,self.user,self.city];
    
    [HTTPTool get:urlStr body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *professionalsArr = dic[@"professionals"];
        
        for (NSDictionary *tempDic in professionalsArr) {
            StylistModel *stylist = [[StylistModel alloc] initWithDic:tempDic];
            //NSLog(@"%@",stylist.realName);
            [self.stylistArr addObject:stylist];
            [stylist release];
            
        }
        
        [self.stylistTableView reloadData];
        [self.stylistTableView footerEndRefreshing];
        
        
    } failure:^(NSError *error) {
       }];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
    self.title = @"设计师";
    
#pragma mark 系统自带的修改title的字体颜色的方法 然并不推荐使用
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
      self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    
    // stylistTableView的创建
    self.stylistTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49-64) style:0];
    [self.view addSubview:self.stylistTableView];
    self.stylistTableView.dataSource = self;
    self.stylistTableView.delegate = self;
    self.stylistTableView.rowHeight = 110;
    self.stylistTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.stylistTableView addFooterWithCallback:^{
        self.start = self.start+5;
        [self createData];
    }];
    
}

#pragma mark // stylistTableView 的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.stylistArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    StylistListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[StylistListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    
    StylistModel *stylist = self.stylistArr[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[stylist.userImage objectForKey:@"large"]]];
        cell.nameLabel.text = stylist.userName;
    NSString *sexStr = [NSString string];
    if ([stylist.sex intValue]==0) {
        sexStr = @"女";
    }else{
        sexStr = @"男";
    }
    
    NSString *informationStr = [NSString stringWithFormat:@"%@ | %@ | %@  %@",stylist.realName,sexStr,stylist.province,stylist.city];
    cell.fourinformationLabel.text = informationStr;
    cell.photoNumLabel.text = [stylist.photoNum stringValue];
    cell.projectNumLabel.text = [stylist.projectNum stringValue];
    cell.ideabookNumLabel.text = [stylist.ideabookNum stringValue];
    return cell;
}


#pragma mark 跳转页面的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StylistDetailViewController *styDetailVC = [[StylistDetailViewController alloc] init];
    StylistModel *stylist = self.stylistArr[indexPath.row];
    styDetailVC.myUserName = stylist.userName;
    [self.navigationController pushViewController:styDetailVC animated:YES];
    [styDetailVC release];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
